<template>
  <el-main>
    <el-form :model="searchForm" label-width="80px" :inline="true" size="small">
      <el-form-item label="">
        <el-input v-model="searchForm.collegeName" placeholder="请输入学院名称"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-search" @click="searchBtn">搜索</el-button>
        <el-button icon="el-icon-close" @click="resetBtn">重置</el-button>
        <el-button icon="el-icon-plus" type="success" @click="addBtn">新增学院</el-button>
      </el-form-item>
    </el-form>

    <el-table :height="tableHeight" :data="tableList" border stripe>
      <el-table-column prop="collegeName" label="学院名称"></el-table-column>
      <el-table-column prop="orderNum" label="学院序号"></el-table-column>
      <el-table-column  label="操作" align="center" width="300">
        <template slot-scope="scope">
          <el-button type="primary" icon="el-icon-edit"  plain size="small" @click="editBtn(scope.row)">编辑</el-button>
          <el-button type="danger" icon="el-icon-delete" plain size="small" @click="deleteBtn(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChange"
      @current-change="currentChange"
      :current-page.sync="searchForm.currentPage"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="searchForm.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="searchForm.total" background>
    </el-pagination>

    <!--    新增弹框-->
    <sys-dialog
      :title="addDialog.title"
      :visible="addDialog.visible"
      :width="addDialog.width"
      :height="addDialog.height"
      @onClose="onClose"
      @onConfirm="onConfirm"
    >
      <div slot="content">
        <el-form :model="addModel" ref="addForm" :rules="rules" label-width="80px" :inline="false" size="small">
          <el-form-item label="学院名称" prop="collegeName">
            <el-input v-model="addModel.collegeName" placeholder="请输入学院名称"></el-input>
          </el-form-item>
          <el-form-item label="序号">
            <el-input v-model="addModel.orderNum" placeholder="请输入学院序号"></el-input>
          </el-form-item>
        </el-form>

      </div>
    </sys-dialog>
  </el-main>
</template>

<script>
//这是弹窗组件
import SysDialog from '@/components/dialog/StstemDialog.vue'
import {addApi, editApi,deleteApi, listApi} from "@/api/college"
import {deleteRoleApi} from "@/api/role";
export default {
  components: {
    SysDialog
  },
  data() {
    return {
      tableHeight: window.innerHeight - 210,
      tableList:[],
      rules:{
        collegeName: [{trigger: 'blur', required: true, message: '请填写学院名称'}],

      },
      //表单绑定
      addModel:{
        type:'',
        collegeId:'',
        collegeName:'',
        orderNum:'',
      },
      addDialog:{title:'',height:150,width:500,visible:false},
      searchForm: {
        collegeName: '',
        currentPage: 1,
        pageSize: 10,
        total:0
      },
    }
  },
  methods: {
    sizeChange(val) {
      this.searchForm.pageSize=val
      this.getList()
    },
    currentChange(val) {
      this.searchForm.currentPage=val
      this.getList()
    },
    editBtn(row){
      this.$resetForm('addForm',this.addModel)
      this.$objCopy(row, this.addModel)
      this.addModel.type='1'
      this.addDialog.title='编辑学院'
      this.addDialog.visible=true
    },
    async deleteBtn(row){
      console.log(row)
      let confirm = await this.$myconfirm("确定删除该学院吗？")
      if (confirm){
        let res = await deleteApi({collegeId: row.collegeId})
        if (res && res.code == 200) {
          //信息提示
          this.$message.success(res.msg)
          //刷新表 格
          this.getList()
        }
      }
    },
    async getList(){
      let res=await listApi(this.searchForm)
      if (res && res.code==200 ){
        console.log(res)
        this.tableList=res.data.records
        this.searchForm.total=res.data.total
      }
    },
    searchBtn() {
      this.getList()
    },
    resetBtn() {
      this.searchForm.currentPage=1
      this.searchForm.collegeName=''
      this.getList()
    },
    addBtn() {
      this.$resetForm('addForm',this.addModel)
      this.addDialog.title="新增学院"
      this.addDialog.visible=true
      this.addModel.type='0'
    },
    onClose(){
      this.addDialog.visible=false
    },
    onConfirm(){
      this.$refs.addForm.validate(async (valid)=>{
        if (valid){
          let res=null
          if (this.addModel.type == '0'){
            res=await addApi(this.addModel)
          }else {
            res=await editApi(this.addModel)
          }
          if (res && res.code==200){
            this.$message.success(res.msg)
            this.addDialog.visible=false
            await this.getList()
          }
        }
      })

    },

  },
  created() {
    this.getList()
  }
}
</script>

<style lang="scss" scoped>

</style>
